package com.ctrip.cap.client.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ws.rs.core.GenericType;

import com.ctrip.cap.client.impl.base.BaseClient;
import com.ctrip.cap.client.impl.base.BaseClientImpl.HttpMethodType;
import com.ctrip.cap.domain.LogRun;

public class CapRunServiceClientImpl extends BaseClient{
	private final static String BASE_PATH = "/run/service";
	private final static String BUILDING_LIST = "/building-list";
	private final static String PROGRESS = "/getProgress";
	private final static String PROGRESS_LIST = "/getProgressList";
	private final static String UPDATE_PROGRESS = "/updateProgress";
	private final static String NOT_SEND_MAIL = "/getNotSendMailRun";
	private final static String NOT_SEND_MAIL_BY_APPID = "/getNotSendMailRunByAppId";
	private final static String UPDATE_SEND_MAIL = "/updateMailStatus";
	
	public class Progress {
		private float progress;
		private float runId;
		
		public Progress() {}

		public float getProgress() {
			return progress;
		}

		public void setProgress(float progress) {
			this.progress = progress;
		}

		public float getRunId() {
			return runId;
		}

		public void setRunId(float runid) {
			this.runId = runid;
		}
		
	}
	
	public CapRunServiceClientImpl() {
		baseUrl = "http://localhost:8080/cap-data-server";
	}
	
	public CapRunServiceClientImpl(String jenkinsServer) {
		baseUrl = jenkinsServer;
	}
	
	public List<Integer> getBuildingRun(int appId) {
		return this.baseCall(BASE_PATH+BUILDING_LIST, appId , HttpMethodType.GET, ArrayList.class, "appId");
	}
	
	public float getBuildingProgress(int runId) {
		return this.baseCall(BASE_PATH+PROGRESS, runId , HttpMethodType.POST, Float.class);
	}
	
	public List<Progress> getBuildingProgressList(List<Integer> runId) {
		return this.baseCall(BASE_PATH+PROGRESS_LIST, runId , HttpMethodType.POST, ArrayList.class);
	}
	
	public List<LogRun> getNotSendMailRun() {
		LogRun[] result = this.baseCall(BASE_PATH + NOT_SEND_MAIL, null, HttpMethodType.GET, LogRun[].class);
		return Arrays.asList(result);
	}
	
	public List<LogRun> getNotSendMailRunByAppId(List<Integer> appIds) {
		LogRun[] result = this.baseCall(BASE_PATH + NOT_SEND_MAIL_BY_APPID, appIds, HttpMethodType.POST, LogRun[].class);
		return Arrays.asList(result);
	}
	
	public void updateProgress(int runId, float progress) {
		LogRun logRun = new LogRun();
		logRun.setRunId(runId);
		logRun.setProgress(progress);
		this.baseCall(BASE_PATH+UPDATE_PROGRESS, logRun , HttpMethodType.POST);
	}
	
	public void updateSendMailStatus(long runid) {
		this.baseCall(BASE_PATH+UPDATE_SEND_MAIL, runid , HttpMethodType.GET, null, "runId");
	}
	
	public static void main(String[] args) {
		CapRunServiceClientImpl cImpl = new CapRunServiceClientImpl();
		//List<Integer> list = cImpl.getBuildingRun(9999);
//		List<Integer> runids = new ArrayList<Integer>();
//		runids.add(10615);
//		runids.add(10616);
//		System.out.println(cImpl.getBuildingProgressList(runids));
		//System.out.println(list);
		//cImpl.updateProgress(10668, 0);
		List<Integer> appIds = new ArrayList<Integer>();
		appIds.add(9999);
		appIds.add(9998);
		cImpl.updateSendMailStatus(11387);
		//System.out.println(result.get(0).getRunId());
	}
	
}
